import { message } from 'antd';
import * as qualityServices from '../services/quality';


export default {
  namespace: 'qualityreport',
  state: {
    ReportInfo: [],
    // ReporterList: [],
    ProjectList: [],
    RelatedAnalysisData:[], // 存储存放查询出来的统计数据
  },
  reducers: { // 同步数据，修改state值
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: { //异步调用
    // 获取质量报告
    *fetchQualityRep({ payload: { params } }, { call, put }) {
      const { data } = yield call(qualityServices.getQualityReport, params);// 接口调用
      const errorMes = data.msg || '查询失败';
      if (data.status !== 200) {
        message.error(errorMes);
      }
      yield put({
        type: 'save',
        payload: {
          ReportInfo: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    // 获取统计数据
    *fetchPrjProcessData({ payload: { params } }, { call, put }) {
      const { data } = yield call(qualityServices.getPrjProcessData, params);// 接口调用
      yield put({
        type: 'save',
        payload: {
          RelatedAnalysisData: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });

    },
    // *fetchReporterList({ payload }, { call, put }) {
    //   const { data } = yield call(qualityServices.getReporterList);// 接口调用
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       ReporterList: data.data || [], // 逻辑表达式，前面为空则用后面的值
    //     },
    //   });
    // },
    *fetchProjectList({ payload }, { call, put }) {
      const { data } = yield call(qualityServices.GetProjectList);// 接口调用
      yield put({
        type: 'save',
        payload: {
          ProjectList: data.data.objects || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *updateQuality({ payload: { qa_rep_param, cb } }, { call,put }) {
      const { data } = yield call(qualityServices.updateQuality, qa_rep_param);
      const errorMes = data.msg || '编辑失败';
      if (data.status === 200) {
        message.success('编辑成功！');
        if (cb) cb();
        yield put({
          type:"fetchQualityRep",
          payload:{
            params:{
              project_name:"",
              fix_version:"",
              reporter:"",
            },
          }
        })
      } else {
        message.error(errorMes);
      }
    },
    *addQuality({ payload: { qa_rep_param, cb } }, { call ,put }) {
      const { data } = yield call(qualityServices.addQuality, qa_rep_param);
      const errorMes = data.msg || '新增失败';
      if (data.status === 200) {
        message.success('新增成功！');
        if (cb) cb();
        yield put({
          type: "fetchQualityRep",
          payload:{
            params:{
              project_name:"",
              fix_version:"",
              reporter:"",
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },

    *saveAndsendQuality({ payload: { sendparam, cb } }, { call,put }) {
      const { data } = yield call(qualityServices.sendQuality, sendparam);
      const errorMes = data.msg || '发送失败';
      if (data.status === 200) {
        message.success('发送成功！');
        if (cb) cb();
        yield put({
          type: "fetchQualityRep",
          payload:{
            params:{
              project_name:"",
              fix_version:"",
              reporter:"",
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *clearQuaAnalysisData({payload},{call , put}){
      yield put({
        type: 'save',
        payload: {
          RelatedAnalysisData:  [], // 逻辑表达式，前面为空则用后面的值
        },
      });

    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/prjmanage/qualityAnalysis') {
          dispatch({ type: 'fetchProjectList' });
          dispatch({ type: 'fetchReporterList' });
          dispatch({ 
           type: 'fetchQualityRep',
           payload:{
            params:{
              project_name:"",
              fix_version:"",
              reporter:"",
            },
          },});
        }
        // dispatch({ type: 'fetchVersionPlan' });
      });
    },
  },
};
